package org.infodavid.common.persistence;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;

import org.infodavid.common.dto.IPersonListItem;
import org.infodavid.common.impl.model.criteria.PersonCriteriaImpl;
import org.infodavid.common.model.IPerson;
import org.infodavid.common.model.ModelFactory;
import org.infodavid.common.model.PersonTest;
import org.infodavid.common.model.components.IEMail;
import org.infodavid.common.model.criteria.IPersonCriteria;

/**
 * The Class PersonDataServiceTest.
 */
@SuppressWarnings({
	"null"
})
public class PersonDataServiceTest extends AbstractDataServiceTest<IPersonListItem,IPerson,Long> {

	/**
	 * The Constructor.
	 */
	public PersonDataServiceTest() {
		super(new PersonTest(), PersonDataService.getInstance());
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see
	 * org.infodavid.common.persistence.AbstractDataServiceTest#modify(org.infodavid.common.model.IDataObject
	 * )
	 */
	@Override
	public void modify(final IPerson item) throws InstantiationException {
		item.setComment("ModifiedComment");
		item.setName("ModifiedName");

		final IEMail mail = ModelFactory.getInstance().create(IEMail.class);

		mail.setValue(item.getName() + '.' + System.nanoTime() + "@domain.com");
		item.getEmails().clear();
		item.getEmails().add(mail);
	}

	/**
	 * Test get instance.
	 */
	@Test
	public final void testGetInstance() {
		Assert.assertEquals("Not a singleton", PersonDataService.getInstance(),
		    PersonDataService.getInstance());
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteria() throws Exception {
		final List<IPersonListItem> results = new ArrayList<IPersonListItem>();
		final IPersonCriteria criteria = new PersonCriteriaImpl();
		final long count = ((PersonDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingAllCriterions() throws Exception {
		final Collection<IPerson> inserted = getTestData();
		final IPerson person = inserted.iterator().next();
		final List<IPersonListItem> results = new ArrayList<IPersonListItem>();
		final IPersonCriteria criteria = new PersonCriteriaImpl();

		criteria.setNamePattern(person.getName());

		final long count = ((PersonDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", 1, results.size());
		Assert.assertEquals("Not all data has been found", 1, count);
		Assert.assertEquals("Wrong data found", person, results.get(0));
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNameNoDataFound() throws Exception {
		final List<IPersonListItem> results = new ArrayList<IPersonListItem>();
		final IPersonCriteria criteria = new PersonCriteriaImpl();

		criteria.setNamePattern("NoData");

		final long count = ((PersonDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", 0, results.size());
		Assert.assertEquals("Not all data has been found", 0, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNameEqual() throws Exception {
		final Collection<IPerson> inserted = getTestData();
		final List<IPersonListItem> results = new ArrayList<IPersonListItem>();
		final IPersonCriteria criteria = new PersonCriteriaImpl();

		criteria.setNamePattern(inserted.iterator().next().getName());

		final long count = ((PersonDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", 1, results.size());
		Assert.assertEquals("Not all data has been found", 1, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNameLike() throws Exception {
		final List<IPersonListItem> results = new ArrayList<IPersonListItem>();
		final IPersonCriteria criteria = new PersonCriteriaImpl();

		criteria.setNamePattern("item%");

		final long count = ((PersonDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNullCollection() throws Exception {
		final IPersonCriteria criteria = new PersonCriteriaImpl();
		Exception exception = null;

		try {
			((PersonDataService)getService()).find(getSession(), null, criteria);
		}
		catch (final Exception e) {
			exception = e;
			e.printStackTrace();
		}

		Assert.assertNotNull("No exception thrown", exception);
		Assert.assertEquals("Wrong exception thrown", IllegalArgumentException.class,
		    exception.getClass());
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNullCriteria() throws Exception {
		final List<IPersonListItem> results = new ArrayList<IPersonListItem>();
		final long count =
		    ((PersonDataService)getService()).find(getSession(), results, (IPersonCriteria)null);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingPage() throws Exception {
		final List<IPersonListItem> results = new ArrayList<IPersonListItem>();
		final IPersonCriteria criteria = new PersonCriteriaImpl();

		criteria.setPageStart(Long.valueOf(0));
		criteria.setPageSize(Integer.valueOf(5));

		final long count = ((PersonDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", criteria.getPageSize().intValue(),
		    results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByName() throws Exception {
		final Collection<IPerson> inserted = getTestData();
		final List<IPersonListItem> results = new ArrayList<IPersonListItem>();
		final long count =
		    ((PersonDataService)getService()).findByName(getSession(), inserted.iterator().next()
		        .getName(), results, null);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", 1, results.size());
		Assert.assertEquals("Not all data has been found", 1, count);
		Assert.assertEquals("Wrong data found", inserted.iterator().next().getName(), results
		    .iterator().next().getName());
		Assert.assertEquals("Wrong data found", inserted.iterator().next().getKey(), results.iterator()
		    .next().getKey());
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByNameUsingNull() throws Exception {
		final List<IPersonListItem> results = new ArrayList<IPersonListItem>();
		Exception exception = null;

		try {
			((PersonDataService)getService()).findByName(getSession(), null, results, null);
		}
		catch (final Exception e) {
			exception = e;
			e.printStackTrace();
		}

		Assert.assertNull("Exception thrown", exception);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByNameUsingNullCollection() throws Exception {
		final Collection<IPerson> inserted = getTestData();
		Exception exception = null;

		try {
			((PersonDataService)getService()).findByName(getSession(), inserted.iterator().next()
			    .getName(), null, null);
		}
		catch (final Exception e) {
			exception = e;
			e.printStackTrace();
		}

		Assert.assertNotNull("No exception thrown", exception);
		Assert.assertEquals("Wrong exception thrown", IllegalArgumentException.class,
		    exception.getClass());
	}
}
